package m.jclecai.com.bean.common;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewStub;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.TextView;

import m.jclecai.com.R;
import m.jclecai.com.view.common.TrendView;

/**
 * Created by Dinosa on 2017/11/3.
 *
 * 这里由于RecyclerView每个条目的显示比较复杂，所以我们将这个条目抽离出来
 */
public class TrendItemView extends FrameLayout {

    private TextView mTvMissDataAvg;
    private TextView mTvMissDataMax;
    private TextView mTvMissDataCur;
    private TextView mTvOutPercent;

    private CheckBox mCbCodes;
    private TrendView mTrendView;
    private ViewStub mViewStub;

    public boolean mIsShowTrend=false;
    private MissDataTrendBean.MisscodesTreanBean mMisscodesTreanBean;

    public TrendItemView(@NonNull Context context) {
        this(context,null);
    }

    public TrendItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TrendItemView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.item_layout_trend,this);

        initView();
    }

    private void initView() {
        mTvMissDataAvg = getView(R.id.tv_missdata_avg);
        mTvMissDataMax = getView(R.id.tv_missdata_max);
        mTvMissDataCur = getView(R.id.tv_missdata_cur);
        mTvOutPercent = getView(R.id.tv_out_percent);
        mCbCodes=getView(R.id.cb_code);

       mViewStub =getView(R.id.view_stub);

        mTvOutPercent.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mIsShowTrend=!mIsShowTrend;
                mMisscodesTreanBean.setOpen(mIsShowTrend);
                //优化布局懒加载；
                if(mViewStub.getParent()!=null){
                    mViewStub.inflate();
                    mTrendView=getView(R.id.trend_view);
                    mTrendView.setMissDataTrendBean(mMisscodesTreanBean);
                }else{
                    mTrendView.setVisibility(mIsShowTrend?View.VISIBLE:View.GONE);
                }

            }
        });
        mCbCodes.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //这里表示我们选中了一注；
                mMisscodesTreanBean.setSelected(mCbCodes.isChecked());
                if (mListener != null) {
                    mListener.onCodeChosed();
                }
            }
        });
    }

    public <T> T getView(int resoureId){
        return ((T) findViewById(resoureId));
    }

    /**
     * 这里我们对数据进行一个填充；
     */
    public void setData(MissDataTrendBean.MisscodesTreanBean misscodesTreanBean){
        mMisscodesTreanBean = misscodesTreanBean;

        mCbCodes.setText(misscodesTreanBean.getCodes());
        mTvMissDataMax.setText(misscodesTreanBean.getMax());
        mTvMissDataAvg.setText(misscodesTreanBean.getAve());
        mTvMissDataCur.setText(misscodesTreanBean.getCur());
        mTvOutPercent.setText(misscodesTreanBean.getOut());

        if(mTrendView!=null){
            mTrendView.setMissDataTrendBean(misscodesTreanBean);
            mTrendView.setVisibility(View.GONE);
            if (mMisscodesTreanBean.isOpen()) {
                mTrendView.setVisibility(View.VISIBLE);
            }
        }
        mCbCodes.setChecked(mMisscodesTreanBean.isSelected());
    }

    public interface OnCodeChosedLisener{
        public void onCodeChosed();
    }

    public OnCodeChosedLisener mListener;

    public void setOnCodeChosedLisener(OnCodeChosedLisener listener) {
        mListener = listener;
    }
}
